<template>
  <div class="app-container">
    <!-- 历史记录界面 -->
    <header class="app-header">
      <el-form inline label-width="140px">
        <el-form-item label="品牌：" prop="brand">
          <el-input v-model.trim="query.brand" clearable @clear="selected" />
        </el-form-item>
        <el-form-item label="名称：" prop="materialName">
          <el-input
            v-model.trim="query.materialName"
            clearable
            @clear="selected"
          />
        </el-form-item>
        <el-form-item label="内部订单单号：" prop="innerCode">
          <el-input
            v-model.trim="query.innerCode"
            clearable
            @clear="selected"
          />
        </el-form-item>
        <el-form-item label="规格：" prop="specifications">
          <el-input
            v-model.trim="query.specifications"
            clearable
            @clear="selected"
          />
        </el-form-item>
        <el-form-item label="图号：" prop="chartNo">
          <el-input v-model.trim="query.chartNo" clearable @clear="selected" />
        </el-form-item>
        <el-form-item label="操作类型：" prop="operator">
          <el-input v-model.trim="query.operator" clearable @clear="selected" />
        </el-form-item>
        <el-form-item label="物料类型：" prop="materialType">
          <el-select v-model="query.materialType" clearable @clear="selected">
            <el-option
              v-for="(label, key) in materialTypeMap"
              :key="key"
              :label="label"
              :value="key"
              >{{ label }}</el-option
            >
          </el-select>
        </el-form-item>
        <el-form-item label="采购类型：" prop="purchaseType">
          <el-select v-model="query.purchaseType" clearable @clear="selected">
            <el-option
              v-for="(label, key) in purchaseTypeMap"
              :key="key"
              :label="label"
              :value="key"
              >{{ label }}</el-option
            >
          </el-select>
        </el-form-item>
      </el-form>

      <div class="establish">
        <el-button type="primary" size="small" @click="selected"
          >查询</el-button
        >
        <el-button plain size="small" @click="reset">重置</el-button>
      </div>
    </header>

    <el-table
      :data="list"
      v-loading="loading"
      :header-row-style="{
        'background-color': '#E6E6E6',
        'font-weight': 'bold',
        color: '#333'
      }"
      :tree-props="{
        children: 'children',
        hasChildren: 'hasChildren'
      }"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
    >
      <el-table-column
        show-overflow-tooltip
        align="center"
        width="150"
        prop="innerCode"
        label="内部单号"
      />

      <el-table-column
        show-overflow-tooltip
        align="center"
        prop="chartNo"
        width="150"
        label="图号"
      />

      <el-table-column
        show-overflow-tooltip
        align="center"
        prop="brand"
        label="品牌"
      />

      <el-table-column
        show-overflow-tooltip
        align="center"
        prop="materialName"
        label="物料名称"
        width="200"
      />

      <el-table-column
        show-overflow-tooltip
        align="center"
        prop="materialType"
        label="物料类型"
      >
        <template slot-scope="scope">
          {{ materialTypeMap[scope.row.materialType] || "--" }}
        </template>
      </el-table-column>

      <el-table-column
        show-overflow-tooltip
        align="center"
        prop="materialType"
        label="采购类型"
      >
        <template slot-scope="scope">
          {{
            scope.row.materialType == 2
              ? purchaseTypeMap[scope.row.purchaseType]
              : "--"
          }}
        </template>
      </el-table-column>

      <el-table-column
        show-overflow-tooltip
        align="center"
        prop="specifications"
        label="规格"
      />

      <el-table-column
        show-overflow-tooltip
        align="center"
        prop="applyAmount"
        label="申请数量"
      />

      <el-table-column
        show-overflow-tooltip
        align="center"
        prop="qualifyAmount"
        label="品检数量"
      />

      <el-table-column
        show-overflow-tooltip
        align="center"
        prop="unQualifyAmount"
        label="不合格数量"
        width="120"
      />

      <el-table-column
        show-overflow-tooltip
        align="center"
        prop="unQualityReason"
        label="不合格原因"
        width="220"
      />

      <el-table-column
        show-overflow-tooltip
        align="center"
        prop="unQualityAttachment"
        label="不合格附件"
        width="120"
      >
        <template slot-scope="scope">
          <a
            v-if="scope.row.unQualityAttachment"
            :href="scope.row.unQualityAttachment"
            target="_blank"
          >
            <el-button type="text" size="mini">
              下载
            </el-button>
          </a>
        </template>
      </el-table-column>

      <el-table-column
        show-overflow-tooltip
        align="center"
        prop="unit"
        label="单位"
      />

      <el-table-column
        show-overflow-tooltip
        align="center"
        prop="operator"
        label="操作者"
      />

      <el-table-column
        show-overflow-tooltip
        align="center"
        width="200"
        prop="operateTime"
        label="操作时间"
      />
    </el-table>

    <el-pagination
      :current-page="query.page"
      :total="total"
      :page-size="query.limit"
      style="padding: 30px 0; text-align: end"
      layout="total, prev, pager, next,jumper"
      @current-change="fetchData"
    />
  </div>
</template>

<script>
import user from "../../api/acl/user.js";
import {
  materialTypeMap,
  purchaseTypeMap,
  operateTypeMap
} from "../../utils/const";
export default {
  name: "history",

  data() {
    return {
      query: {
        brand: "",
        materialName: "",
        specifications: "",
        materialType: "",
        purchaseType: "",
        innerCode: "",
        chartNo: "",
        operator: "",
        page: 1,
        limit: 10
      },
      list: [],
      total: 1,
      loading: false,
      materialTypeMap,
      purchaseTypeMap,
      operateTypeMap
    };
  },

  created() {
    this.getList();
  },

  methods: {
    reset() {
      this.query = {
        brand: "",
        materialName: "",
        specifications: "",
        purchaseType: "",
        materialType: "",
        chartNo: "",
        page: 1,
        limit: 10
      };
      this.getList();
    },
    selected() {
      this.getList();
    },
    fetchData(val) {
      this.query.page = val;
      this.getList();
    },
    getList() {
      let query = Object.assign({}, this.query, {
        operateType: this.query.operateType == 0 ? false : true
      });
      this.loading = true;
      user.qualityListRecord(query).then(response => {
        this.total = response.total;
        this.loading = false;
        this.list = response.list;
      });
    }
  }
};
</script>

<style>
.establish {
  text-align: right;
}
</style>
